package other.mvvm.activity.src.app_package.login

fun loginViewModel(
        packageName:String,
        activityClass:String
)="""
package ${packageName}
import cn.logicalthinking.mvvm.base.BaseViewModel
import androidx.lifecycle.MutableLiveData

class ${activityClass}ViewModel:BaseViewModel<${activityClass}Repository>() {
    override fun initRepository()= ${activityClass}Repository()
    
    
   /**
     *  登录
     * @param  
     */
    fun login(account: String, password: String, success: () -> Unit = {}) {
 
        viewModelScope.launch {
            mRepository.doLogin(account, password)
                .onStart { 
                    // 请求开始之前的操作
                    isLoading.value = true
                }
                .catch {
                    // 捕获并处理上游抛出的异常
                    isLoading.value = false
                }
                .onCompletion {
                    // 已完成
                    isLoading.value = false
                }
                .collectLatest {
                    // 获取结果
                    it.doSuccess {
                        //保存账号
                        SPUtils.getInstance().put("account", account)
                        SPUtils.getInstance().setToken(it)
                        success()
                    }
                    it.doFailure {
                        ToastUtils.showShort(it?.message)
                    }

                }
        }
    }

    
}
"""